
 <!DOCTYPE HTML>
<html >
<head>
  <meta charset="UTF-8">
  
    <title>springboot之session集成redis | Typhoon&#39;s code space</title>
    <meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=no">
    
    <meta name="author" content="typhoon">
    

    
    <meta name="description" content="背景介绍    我们正常境况下写的应用部署到服务器中，session会话是默认存储到服务器内存中，内存的优点是读写比较快， 正如jvm内存是java世界中最快的内存一样，但是任何事物都有其两面性，内存session的缺点和优点一样明显，">
<meta property="og:type" content="article">
<meta property="og:title" content="springboot之session集成redis">
<meta property="og:url" content="http://scorpioaeolus.oschina.io/2019/03/01/springboot之session集成redis/index.html">
<meta property="og:site_name" content="Typhoon&#39;s code space">
<meta property="og:description" content="背景介绍    我们正常境况下写的应用部署到服务器中，session会话是默认存储到服务器内存中，内存的优点是读写比较快， 正如jvm内存是java世界中最快的内存一样，但是任何事物都有其两面性，内存session的缺点和优点一样明显，">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2019/03/01/springboot之session集成redis/s1.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2019/03/01/springboot之session集成redis/s2.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2019/03/01/springboot之session集成redis/s3.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2019/03/01/springboot之session集成redis/s4.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2019/03/01/springboot之session集成redis/s5.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2019/03/01/springboot之session集成redis/s6.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2019/03/01/springboot之session集成redis/s7.png">
<meta property="og:image" content="http://scorpioaeolus.oschina.io/typhoon/2019/03/01/springboot之session集成redis/s8.png">
<meta property="og:updated_time" content="2019-03-01T12:55:13.121Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="springboot之session集成redis">
<meta name="twitter:description" content="背景介绍    我们正常境况下写的应用部署到服务器中，session会话是默认存储到服务器内存中，内存的优点是读写比较快， 正如jvm内存是java世界中最快的内存一样，但是任何事物都有其两面性，内存session的缺点和优点一样明显，">
<meta name="twitter:image" content="http://scorpioaeolus.oschina.io/typhoon/2019/03/01/springboot之session集成redis/s1.png">

    
    <link rel="alternative" href="/atom.xml" title="Typhoon&#39;s code space" type="application/atom+xml">
    
    
    <link rel="icon" href="/typhoon/img/java.png">
    
    
    <link rel="apple-touch-icon" href="/typhoon/img/jacman.jpg">
    <link rel="apple-touch-icon-precomposed" href="/typhoon/img/jacman.jpg">
    
    <link rel="stylesheet" href="/typhoon/css/style.css">
</head>

  <body>
    <header>
      
<div>
		
			<div id="imglogo">
				<a href="/typhoon/"><img src="/typhoon/img/java.png" alt="Typhoon&#39;s code space" title="Typhoon&#39;s code space"/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/typhoon/" title="Typhoon&#39;s code space">Typhoon&#39;s code space</a></h1>
				<h2 class="blog-motto"></h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="Menu">
			</a></div>
			<nav class="animated">
				<ul>
					<ul>
					 
						<li><a href="/typhoon/">主页</a></li>
					
						<li><a href="/typhoon/archives">归档</a></li>
					
						<li><a href="/typhoon/about">关于</a></li>
					
					<li>
 					
					<form class="search" action="//google.com/search" method="get" accept-charset="utf-8">
						<label>Search</label>
						<input type="search" id="search" name="q" autocomplete="off" maxlength="20" placeholder="Search" />
						<input type="hidden" name="q" value="site:scorpioaeolus.oschina.io">
					</form>
					
					</li>
				</ul>
			</nav>			
</div>
    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
  
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/typhoon/2019/03/01/springboot之session集成redis/" title="springboot之session集成redis" itemprop="url">springboot之session集成redis</a>
  </h1>
  <p class="article-author">By
       
		<a href="/typhoon/about" title="typhoon" target="_blank" itemprop="author">typhoon</a>
		
  <p class="article-time">
    <time datetime="2019-03-01T12:36:47.000Z" itemprop="datePublished"> Published 2019-03-01</time>
    
  </p>
</header>
	<div class="article-content">
		
		<div id="toc" class="toc-article">
			<strong class="toc-title">Contents</strong>
		
			<ol class="toc"><li class="toc-item toc-level-5"><a class="toc-link" href="#背景介绍"><span class="toc-number">1.</span> <span class="toc-text">背景介绍</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#问题抛出"><span class="toc-number">2.</span> <span class="toc-text">问题抛出</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#会话分离存储"><span class="toc-number">3.</span> <span class="toc-text">会话分离存储</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#session会话共享"><span class="toc-number">4.</span> <span class="toc-text">session会话共享</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#总结"><span class="toc-number">5.</span> <span class="toc-text">总结</span></a></li></ol>
		
		</div>
		
		<h5 id="背景介绍"><a href="#背景介绍" class="headerlink" title="背景介绍"></a>背景介绍</h5><pre><code>    我们正常境况下写的应用部署到服务器中，session会话是默认存储到服务器内存中，内存的优点是读写比较快，
正如jvm内存是java世界中最快的内存一样，但是任何事物都有其两面性，内存session的缺点和优点一样明显，
</code></pre><a id="more"></a>
<pre><code>这里我们简要分析一下内存session的优缺点：
</code></pre><img src="/typhoon/2019/03/01/springboot之session集成redis/s1.png" alt="session" title="session">
<ul>
<li>优点<br>  读取快<br>  不存在一致性问题(单点)    </li>
<li><p>缺点<br>  当会话量足够大时，会打爆应用内存<br>  无法做集群</p>
<pre><code>由于传统的单体架构已经远远支撑不了当前互联网井喷式发展所带来的巨大流量，所以目前新的应用架构一定会考虑分布式和集群，
</code></pre><p>  那么会面临很多之前单体架构不存在的问题和挑战。    </p>
<pre><code>比较明显的就是：    
</code></pre></li>
<li>session共享</li>
<li>请求路由</li>
<li><p>数据路由</p>
<pre><code>我们用一张图来描述传统的单体架构简单粗暴的换成集群部署锁面临的问题：
</code></pre><img src="/typhoon/2019/03/01/springboot之session集成redis/s2.png" alt="session" title="session">
</li>
</ul>
<p>1.用户请求过来后如何确定路由到哪台服务器上？<br>2.用户请求落到多台服务器上是不是要每台服务器都存储一份会话？<br>3.用户读写操作如何路由到指定的DB？</p>
<pre><code>对于上述三个问题，本篇不会做全部解答，会主要针对问题2做详细分析和介绍。
</code></pre><h5 id="问题抛出"><a href="#问题抛出" class="headerlink" title="问题抛出"></a>问题抛出</h5><pre><code>针对上边描述的问题2，细化衍生成两个问题：
</code></pre><p>1.session分离存储问题<br>2.session共享问题<br>         接下来我们将通过代码和实际案例来解答上述问题，并实现使用redis存储会话和解决会话共享问题。</p>
<h5 id="会话分离存储"><a href="#会话分离存储" class="headerlink" title="会话分离存储"></a>会话分离存储</h5><pre><code>   传统的单体架构，在会话量暴增的时候，可能会导致应用内存爆掉，导致jvm内存溢出，那么单纯解决这个问题相对比较简单，
我们可以把session从应用内存中抽出来单独存储。
   首先先看一下应用内存session存储模式：
</code></pre><img src="/typhoon/2019/03/01/springboot之session集成redis/s3.png" alt="session" title="session">
<pre><code>并发量大的时候，红色区域占用存储空间会暴增，从而打爆应用内存，导致应用崩溃。
再看一下会话分离存储模式：
</code></pre><img src="/typhoon/2019/03/01/springboot之session集成redis/s4.png" alt="session" title="session">
<pre><code>session会话会单独存储到外部存储器中，这样会话量暴涨的时候，只会占用redis存储空间，不会对应用造成影响。
</code></pre><p><strong>实现</strong></p>
<p>1.新建spingboot应用&amp;添加依赖<br><figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">&lt;!-- redis --&gt;</div><div class="line">       &lt;dependency&gt;</div><div class="line">           &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;</div><div class="line">           &lt;artifactId&gt;spring-boot-starter-data-redis&lt;/artifactId&gt;</div><div class="line">       &lt;/dependency&gt;</div><div class="line">       &lt;!-- session-redis --&gt;</div><div class="line">       &lt;dependency&gt;</div><div class="line">           &lt;groupId&gt;org.springframework.session&lt;/groupId&gt;</div><div class="line">           &lt;artifactId&gt;spring-session-data-redis&lt;/artifactId&gt;</div><div class="line">       &lt;/dependency&gt;</div></pre></td></tr></table></figure></p>
<pre><code>启动类：
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@SpringBootApplication</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">App</span></span></div><div class="line">&#123;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</div><div class="line">        SpringApplication.run(App.class, args);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>2.新建application.properties文件<br><figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">#redis</div><div class="line">spring.redis.database=0</div><div class="line">spring.redis.host=host</div><div class="line">spring.redis.port=port</div><div class="line">spring.redis.password=password</div><div class="line"># session</div><div class="line">spring.session.store-type=redis</div></pre></td></tr></table></figure></p>
<p>3.编写session配置<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Configuration</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SessionConfig</span> <span class="keyword">implements</span> <span class="title">WebMvcConfigurer</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addInterceptors</span><span class="params">(InterceptorRegistry registry)</span> </span>&#123;</div><div class="line">        registry.addInterceptor(<span class="keyword">new</span> SecurityInterceptor())</div><div class="line">                <span class="comment">//排除拦截</span></div><div class="line">                .excludePathPatterns(<span class="string">"/user/login"</span>)</div><div class="line">                .excludePathPatterns(<span class="string">"/user/logout"</span>)</div><div class="line">                <span class="comment">//拦截路径</span></div><div class="line">                .addPathPatterns(<span class="string">"/**"</span>);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Configuration</span></div><div class="line">    <span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SecurityInterceptor</span> <span class="keyword">implements</span> <span class="title">HandlerInterceptor</span> </span>&#123;</div><div class="line">        <span class="meta">@Override</span></div><div class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">preHandle</span><span class="params">(HttpServletRequest request, HttpServletResponse response, Object handler)</span> <span class="keyword">throws</span> IOException </span>&#123;</div><div class="line">            HttpSession session = request.getSession();</div><div class="line">            <span class="keyword">if</span> (session.getAttribute(session.getId()) != <span class="keyword">null</span>)&#123;</div><div class="line">                <span class="keyword">return</span> <span class="keyword">true</span>;</div><div class="line">            &#125;</div><div class="line">            response.getWriter().write(JSON.toJSONString(<span class="string">"please login first"</span>));</div><div class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>4.编写业务代码&amp;测试<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@RestController</span></div><div class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/user"</span>)</div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">IndexController</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="meta">@GetMapping</span>(<span class="string">"/login"</span>)</div><div class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">login</span><span class="params">(String account, String password,HttpSession session)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span>(<span class="string">"typhoon"</span>.equalsIgnoreCase(account) &amp;&amp; <span class="string">"123"</span>.equalsIgnoreCase(password)) &#123;</div><div class="line">            session.setAttribute(session.getId(),User.builder()</div><div class="line">                    .account(account)</div><div class="line">                    .password(password)</div><div class="line">                    .build());</div><div class="line">         <span class="keyword">return</span>  <span class="string">"success"</span>;</div><div class="line">        &#125;</div><div class="line">        <span class="keyword">return</span> <span class="string">"failed"</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@GetMapping</span>(value = <span class="string">"/logout"</span>)</div><div class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">logout</span><span class="params">(HttpSession session)</span></span>&#123;</div><div class="line">        session.removeAttribute(session.getId());</div><div class="line">        <span class="keyword">return</span> <span class="string">"user logout success"</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@GetMapping</span>(value = <span class="string">"/&#123;account&#125;"</span>)</div><div class="line">    <span class="function"><span class="keyword">public</span> User <span class="title">getUser</span><span class="params">(@PathVariable(<span class="string">"account"</span>)</span> String account) </span>&#123;</div><div class="line">        <span class="keyword">return</span> User.builder()</div><div class="line">                .account(account)</div><div class="line">               .build();</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<pre><code>请求之前redis中key列表：
</code></pre><img src="/typhoon/2019/03/01/springboot之session集成redis/s5.png" alt="session" title="session">
<pre><code>请求之后redis中key列表：
</code></pre><img src="/typhoon/2019/03/01/springboot之session集成redis/s6.png" alt="session" title="session">
<pre><code>这样我们就基于springboot和redis实现了session会话分离存储。
</code></pre><h5 id="session会话共享"><a href="#session会话共享" class="headerlink" title="session会话共享"></a>session会话共享</h5><pre><code>    session会话分离存储解决了并发场景的应用内存问题，但是在应用集群部署情况下存在session会话共享问题，
基于上一节的实现，如果应用集群部署会存在同一用户请求路由到不同服务器产生多份session问题，造成存储空间浪费：
</code></pre><img src="/typhoon/2019/03/01/springboot之session集成redis/s7.png" alt="session" title="session">
<pre><code>    如果集群服务器共享session会话，那么将会节省很多存储空间，并且省去用户路由到新的服务器上的session重新生成
带来的登录失效问题：
</code></pre><img src="/typhoon/2019/03/01/springboot之session集成redis/s8.png" alt="session" title="session">
<p><strong>实现</strong></p>
<p>1.在上一节SessionConfig代码中增加@EnableRedisHttpSession注解：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Configuration</span></div><div class="line"><span class="meta">@EnableRedisHttpSession</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SessionConfig</span> <span class="keyword">implements</span> <span class="title">WebMvcConfigurer</span> </span>&#123;</div><div class="line"><span class="comment">//</span></div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>2.编写业务代码<br>        在controller中增加以下代码：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@RequestMapping</span>(value = <span class="string">"/first"</span>, method = RequestMethod.GET)  </div><div class="line">    <span class="function"><span class="keyword">public</span> Map&lt;String, Object&gt; <span class="title">firstResp</span> <span class="params">(HttpServletRequest request)</span></span>&#123;  </div><div class="line">        Map&lt;String, Object&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;();  </div><div class="line">        request.getSession().setAttribute(<span class="string">"request Url"</span>, request.getRequestURL());  </div><div class="line">        map.put(<span class="string">"request Url"</span>, request.getRequestURL());  </div><div class="line">        <span class="keyword">return</span> map;  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="meta">@RequestMapping</span>(value = <span class="string">"/sessions"</span>, method = RequestMethod.GET)  </div><div class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">sessions</span> <span class="params">(HttpServletRequest request)</span></span>&#123;  </div><div class="line">        Map&lt;String, Object&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;();  </div><div class="line">        map.put(<span class="string">"sessionId"</span>, request.getSession().getId());  </div><div class="line">        map.put(<span class="string">"message"</span>, request.getSession().getAttribute(<span class="string">"map"</span>));  </div><div class="line">        <span class="keyword">return</span> map;  </div><div class="line">    &#125;</div></pre></td></tr></table></figure></p>
<p>3.打包并启动服务<br>        打包并分别使用两个端口启动服务<br>4.测试验证<br>        启动之后先通过8080端口访问服务，返回地址：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&#123;<span class="string">"request Url"</span>:<span class="string">"http://localhost:8080/user/first"</span>&#125;</div></pre></td></tr></table></figure></p>
<pre><code>接着，我们访问8080端口的sessions，返回：
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&#123;<span class="string">"sessionId"</span>:<span class="string">"efcc85c0-9ad2-49a6-a38f-9004403776b5"</span>,<span class="string">"message"</span>:<span class="string">"http://localhost:8080/user/first"</span>&#125;</div></pre></td></tr></table></figure>
<pre><code>最后，再访问9090端口的sessions，返回：
</code></pre><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&#123;<span class="string">"sessionId"</span>:<span class="string">"efcc85c0-9ad2-49a6-a38f-9004403776b5"</span>,<span class="string">"message"</span>:<span class="string">"http://localhost:8080/user/first"</span>&#125;</div></pre></td></tr></table></figure>
<pre><code>    由此可见，8080与9090两个服务器返回结果一样，实现了session的共享，也就是说通过此种方式可以实现集群
情况下session共享。
</code></pre><h5 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h5><pre><code>    本篇我们介绍了传统session存储方式存在的弊端，以及通过实际案例实现了session分离存储和解决了集群环境下session共享问题，
从宏观层面上也分析了从传统单体架构到目前分布式集群架构所面临和需要解决的问题。
</code></pre>  
	</div>
		<footer class="article-footer clearfix">
<div class="article-catetags">

<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/typhoon/categories/架构/">架构</a>
</div>


</div>



	<div class="article-share" id="share">
	
	  <div data-url="http://scorpioaeolus.oschina.io/2019/03/01/springboot之session集成redis/" data-title="springboot之session集成redis | Typhoon&#39;s code space" data-tsina="null" class="share clearfix">
	  </div>
	
	</div>


</footer>

   	       
	</article>
	
<nav class="article-nav clearfix">
 

<div class="next">
<a href="/typhoon/2019/03/01/聊一聊容器与Docker/"  title="聊一聊容器与Docker">
 <strong>下一篇：</strong><br/> 
 <span>聊一聊容器与Docker
</span>
</a>
</div>

</nav>

	


</div>  
      <div class="openaside"><a class="navbutton" href="#" title="Show Sidebar"></a></div>

  <div id="toc" class="toc-aside">
  <strong class="toc-title">Contents</strong>
 
 <ol class="toc"><li class="toc-item toc-level-5"><a class="toc-link" href="#背景介绍"><span class="toc-number">1.</span> <span class="toc-text">背景介绍</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#问题抛出"><span class="toc-number">2.</span> <span class="toc-text">问题抛出</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#会话分离存储"><span class="toc-number">3.</span> <span class="toc-text">会话分离存储</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#session会话共享"><span class="toc-number">4.</span> <span class="toc-text">session会话共享</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#总结"><span class="toc-number">5.</span> <span class="toc-text">总结</span></a></li></ol>
 
  </div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="Hide Sidebar"></a></div>
<aside class="clearfix">

  


  
<div class="categorieslist">
	<p class="asidetitle">Categories</p>
		<ul>
		
		  
			<li><a href="/typhoon/categories/dubbo/" title="dubbo">dubbo<sup>8</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/java性能优化/" title="java性能优化">java性能优化<sup>17</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/java源码分析/" title="java源码分析">java源码分析<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/motan/" title="motan">motan<sup>2</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/spring/" title="spring">spring<sup>11</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/springboot/" title="springboot">springboot<sup>28</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/springmvc/" title="springmvc">springmvc<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/容器/" title="容器">容器<sup>1</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/工具/" title="工具">工具<sup>7</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/性能优化/" title="性能优化">性能优化<sup>5</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/架构/" title="架构">架构<sup>8</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/案例分析/" title="案例分析">案例分析<sup>13</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/缓存/" title="缓存">缓存<sup>4</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/设计模式/" title="设计模式">设计模式<sup>3</sup></a></li>
		  
		
		  
			<li><a href="/typhoon/categories/面试题/" title="面试题">面试题<sup>1</sup></a></li>
		  
		
		</ul>
</div>


  
<div class="tagslist">
	<p class="asidetitle">Tags</p>
		<ul class="clearfix">
		
			
				<li><a href="/typhoon/tags/springboot/" title="springboot">springboot<sup>28</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/java性能优化/" title="java性能优化">java性能优化<sup>9</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/限流/" title="限流">限流<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/Spring5/" title="Spring5">Spring5<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/设计模式/" title="设计模式">设计模式<sup>3</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/案例分析/" title="案例分析">案例分析<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/性能优化/" title="性能优化">性能优化<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/Spring/" title="Spring">Spring<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo/" title="dubbo">dubbo<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/分页/" title="分页">分页<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/并发/" title="并发">并发<sup>2</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/异常拦截/" title="异常拦截">异常拦截<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo分布式事务/" title="dubbo分布式事务">dubbo分布式事务<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/自带图片/" title="自带图片">自带图片<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/svn导出mvn项目/" title="svn导出mvn项目">svn导出mvn项目<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/dubbo过滤器/" title="dubbo过滤器">dubbo过滤器<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/排序性能对比/" title="排序性能对比">排序性能对比<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/ListenableFuture/" title="ListenableFuture">ListenableFuture<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/PropertyChangeListener/" title="PropertyChangeListener">PropertyChangeListener<sup>1</sup></a></li>
			
		
			
				<li><a href="/typhoon/tags/jdbcTemplate高可用/" title="jdbcTemplate高可用">jdbcTemplate高可用<sup>1</sup></a></li>
			
		
		</ul>
</div>


  <div class="linkslist">
  <p class="asidetitle">Links</p>
    <ul>
        
          <li>
            
            	<a href="https://coderq.com" target="_blank" title="一个面向程序员交流分享的新一代社区">码农圈</a>
            
          </li>
        
          <li>
            
            	<a href="http://wuchong.me" target="_blank" title="Jark&#39;s Blog">Jark&#39;s Blog</a>
            
          </li>
        
    </ul>
</div>

  


  <div class="rsspart">
	<a href="/atom.xml" target="_blank" title="rss">RSS</a>
</div>

  <div class="weiboshow">
  <p class="asidetitle">Weibo</p>
    <iframe width="100%" height="119" class="share_self"  frameborder="0" scrolling="no" src="http://widget.weibo.com/weiboshow/index.php?language=&width=0&height=119&fansRow=2&ptype=1&speed=0&skin=9&isTitle=1&noborder=1&isWeibo=0&isFans=0&uid=null&verifier=b3593ceb&dpc=1"></iframe>
</div>


</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	<div class="line">
		<span></span>
		<div class="author"></div>
	</div>
	
	
	<section class="info">
		<p> Hello ,I&#39;m a java coder. <br/>
			This is my blog,believe it or not.</p>
	</section>
	 
	<div class="social-font" class="clearfix">
		
		
		
		
		
		
		
		
		
		
		<a href="mailto:ScorpioAeolus@163.com" target="_blank" class="icon-email" title="Email Me"></a>
		
	</div>
			
		

		<p class="copyright">
		Powered by <a href="http://hexo.io" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/wuchong/jacman" target="_blank" title="Jacman">Jacman</a> © 2019 
		
		<a href="/typhoon/about" target="_blank" title="typhoon">typhoon</a>
		
		
		</p>
</div>
</footer>
    <script src="/typhoon/js/jquery-2.0.3.min.js"></script>
<script src="/typhoon/js/jquery.imagesloaded.min.js"></script>
<script src="/typhoon/js/gallery.js"></script>
<script src="/typhoon/js/jquery.qrcode-0.12.0.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
  
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else{
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
      
      $('#toc.toc-aside').css('display', 'none');
        
    }
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  c.click(function(){
    ta.css('display', 'block').addClass('fadeIn');
  });
  o.click(function(){
    ta.css('display', 'none');
  });
  $(window).scroll(function(){
    ta.css("top",Math.max(140,320-$(this).scrollTop()));
  });
});
</script>


<script type="text/javascript">
$(document).ready(function(){ 
  var $this = $('.share'),
      url = $this.attr('data-url'),
      encodedUrl = encodeURIComponent(url),
      title = $this.attr('data-title'),
      tsina = $this.attr('data-tsina'),
      description = $this.attr('description');
  var html = [
  '<div class="hoverqrcode clearfix"></div>',
  '<a class="overlay" id="qrcode"></a>',
  '<a href="https://www.facebook.com/sharer.php?u=' + encodedUrl + '" class="article-share-facebook" target="_blank" title="Facebook"></a>',
  '<a href="https://twitter.com/intent/tweet?url=' + encodedUrl + '" class="article-share-twitter" target="_blank" title="Twitter"></a>',
  '<a href="#qrcode" class="article-share-qrcode" title="微信"></a>',
  '<a href="http://widget.renren.com/dialog/share?resourceUrl=' + encodedUrl + '&srcUrl=' + encodedUrl + '&title=' + title +'" class="article-share-renren" target="_blank" title="人人"></a>',
  '<a href="http://service.weibo.com/share/share.php?title='+title+'&url='+encodedUrl +'&ralateUid='+ tsina +'&searchPic=true&style=number' +'" class="article-share-weibo" target="_blank" title="微博"></a>',
  '<span title="Share to"></span>'
  ].join('');
  $this.append(html);

  $('.hoverqrcode').hide();

  var myWidth = 0;
  function updatehoverqrcode(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
    var qrsize = myWidth > 1024 ? 200:100;
    var options = {render: 'image', size: qrsize, fill: '#2ca6cb', text: url, radius: 0.5, quiet: 1};
    var p = $('.article-share-qrcode').position();
    $('.hoverqrcode').empty().css('width', qrsize).css('height', qrsize)
                          .css('left', p.left-qrsize/2+20).css('top', p.top-qrsize-10)
                          .qrcode(options);
  };
  $(window).resize(function(){
    $('.hoverqrcode').hide();
  });
  $('.article-share-qrcode').click(function(){
    updatehoverqrcode();
    $('.hoverqrcode').toggle();
  });
  $('.article-share-qrcode').hover(function(){}, function(){
      $('.hoverqrcode').hide();
  });
});   
</script>









<link rel="stylesheet" href="/typhoon/fancybox/jquery.fancybox.css" media="screen" type="text/css">
<script src="/typhoon/fancybox/jquery.fancybox.pack.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.article-content').each(function(i){
    $(this).find('img').each(function(){
      if ($(this).parent().hasClass('fancybox')) return;
      var alt = this.alt;
      if (alt) $(this).after('<span class="caption">' + alt + '</span>');
      $(this).wrap('<a href="' + this.src + '" title="' + alt + '" class="fancybox"></a>');
    });
    $(this).find('.fancybox').each(function(){
      $(this).attr('rel', 'article' + i);
    });
  });
  if($.fancybox){
    $('.fancybox').fancybox();
  }
}); 
</script>



<!-- Analytics Begin -->



<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?e6d1f421bbc9962127a50488f9ed37d1";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>



<!-- Analytics End -->

<!-- Totop Begin -->

	<div id="totop">
	<a title="Back to Top"><img src="/typhoon/img/scrollup.png"/></a>
	</div>
	<script src="/typhoon/js/totop.js"></script>

<!-- Totop End -->

<!-- MathJax Begin -->
<!-- mathjax config similar to math.stackexchange -->


<!-- MathJax End -->

<!-- Tiny_search Begin -->

<!-- Tiny_search End -->

  </body>
</html>
